package com.projectplace.GUI.adapter;

/*
 * Copyright (c) 2011 Projectplace International AB
 * Permission is hereby granted, free of charge, to any person 
 * obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, 
 * publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:The above copyright notice and this 
 * permission notice shall be included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import java.io.File;
import java.util.HashMap;
import java.util.List;
import com.projectplace.R;
import com.projectplace.SingeltonPPClient;
import com.projectplace.api.UserApi;
import com.projectplace.api.objectResponses.Project;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import android.widget.TextView;

public class Conversation_myProjects_Adapter extends BaseAdapter {

	private List<Project> project;
	
	@SuppressWarnings("unused")
	private final String LOG_TAG = "ConversationsAdapter";
	
	private Activity activity;
	private static LayoutInflater inflater = null;
	private HashMap<String, Bitmap> bitHash;

	public Conversation_myProjects_Adapter(Activity activity,List<Project> project) {
		this.activity = activity;
		this.project = project;

		inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		bitHash = new HashMap<String, Bitmap>();
	}

	public int getCount() {
		return project.size();
	}

	public Project getItem(int position) {
		return project.get(position);
	}

	public long getItemId(int position) {
		return Long.parseLong(project.get(position).getId());
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder hold;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.listrow, parent,false);
			hold = new ViewHolder();
			hold.listitem_pic = (ImageView) convertView
					.findViewById(R.id.imageDoc);
			hold.listitem_title = (TextView) convertView
					.findViewById(R.id.descriptionDoc);
			convertView.setTag(hold);
		} else {
			hold = (ViewHolder) convertView.getTag();
		}

		final Project projConv = (Project) project.get(position);

		if (projConv != null) {
			hold.listitem_title.setText(projConv.getName());
			if(bitHash.containsKey(projConv.getId()))
					hold.listitem_pic.setImageBitmap(bitHash.get(projConv.getId()));
			else{
				Bitmap holder = BitmapFactory.decodeFile(activity.getFilesDir()
						.toString()
						+ File.separatorChar
						+ projConv.getId()
						+ ".png");
				if (holder != null){
					if(holder.isMutable())
						holder.eraseColor(Color.WHITE);
					hold.listitem_pic.setImageBitmap(holder);
					bitHash.put(projConv.getId(), holder);
				}

				else{
					UserApi.getAvatar(SingeltonPPClient.getInstance(),
							projConv.getId(), hold.listitem_pic);
				}
			}
		}
		return convertView;
	}

	static class ViewHolder {
		TextView listitem_title;
		ImageView listitem_pic;
	}
}
